package com.yf.modules.system.manager;

import cn.hutool.core.util.StrUtil;
import com.yf.Utils.RedisUtils;
import com.yf.configuration.SecurityProperties;
import com.yf.modules.security.model.dto.JwtUserDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author chentianwei
 * @date 2024-05-29 17:19
 * @description 用户缓存管理器
 */
@Component
public class UserCacheManager {
    private final RedisUtils redisUtils;

    @Value("${login.user-cache.idle-time}")
    private Long idleTime;

    public UserCacheManager(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    @Async
    public void addUserCache(String userName, JwtUserDto userDto) {
        if (StrUtil.isNotEmpty(userName)) {
            //缓存数据,并设置过期时间
            redisUtils.hset(SecurityProperties.cacheKey, userName, userDto, idleTime);
        }
    }

    @Async
    public void cleanUserCache(String userName) {
        if (StrUtil.isNotEmpty(userName)) {
            //擦除缓存
            redisUtils.hashDel(SecurityProperties.cacheKey, userName);
        }
    }
}
